package com.ruoyi.client.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.recycle.domain.*;
import com.ruoyi.recycle.dto.*;
import com.ruoyi.recycle.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

@RestController
@RequestMapping("/personalCenter")
@Api(tags = "个人中心")
public class PersonalCenterController {

    @Resource
    private IClientUserManagementService clientUserManagementService;
    @Resource
    private ClientCouponService clientCouponService;
    @Resource
    private IOrderEvaluateService orderEvaluateService;
    @Resource
    private IRecycleOrderService recycleOrderService;
    @Resource
    private IHousekeepingOrderService housekeepingOrderService;
    @Resource
    private IIntegralOrderService integralOrderService;
    @Resource
    private IIntegralCommodityService integralCommodityService;
    @Resource
    private IShopManagementService shopManagementService;
    @Resource
    private IMerchantManagementService merchantManagementService;
    @Resource
    private ClientPrayerManagementService clientPrayerManagementService;

    /**
     * 用户充值接口
     * @param paymentDTO
     * @return
     */
    @PostMapping("/charge")
    public AjaxResult charge(@RequestBody PaymentDTO paymentDTO) {
        return clientUserManagementService.charge(paymentDTO);
    }


    /**
     * 用户提现接口
     * @param withDrawDTO
     * @return
     */
    @PostMapping("/withDraw")
    public AjaxResult withDraw(@RequestBody ClientWithDrawDTO withDrawDTO) {

        return clientUserManagementService.withDraw(withDrawDTO);

    }

    /**
     * 我的优惠券
     * @param clientCouponDTO
     * @return
     */
    @PostMapping("/myCoupon")
    public AjaxResult myCoupon(@RequestBody ClientCouponDTO clientCouponDTO) {
        return clientCouponService.myCoupon(clientCouponDTO);
    }

    /**
     * 我的页面数据
     * @param  clientId
     * @return
     */
    @GetMapping("/myHome/{clientId}")
    public AjaxResult myHome(@PathVariable("clientId") Long clientId) {

        return clientUserManagementService.myHome(clientId);
    }

    /**
     * 我的页面数据-切换小学生用户
     * @param openid
     * @param clientType
     * @return
     */
    @PostMapping("/myHomeByClientId")
    public AjaxResult myHome(@RequestParam(value = "openid",required = true) String openid,
                             @RequestParam(value = "clientType",required = true) String clientType) {

        return clientUserManagementService.myHomeByClientId(openid,clientType);
    }

    /**
     * 生成达人海报
     * @param userId
     * @return
     */
    @PostMapping("/generatePoster")
    public AjaxResult generatePoster(@RequestParam(value = "userId",required = true) Integer userId,
                                     @RequestParam(value = "clientType",required = true) Integer clientType) {

        return clientUserManagementService.generatePoster(userId,clientType);
    }

    /**
     * 减碳详情
     * @param clientId
     * @return
     */
    @GetMapping("/carbonReduction/{id}")
    public AjaxResult carbonReduction(@PathVariable("id") Integer clientId) {

        return clientUserManagementService.carbonReduction(clientId);
    }

    /**
     * 查看达人数据
     * @param subordinateDTO
     * @return
     */
    @PostMapping("/mySubordinate")
    public AjaxResult mySubordinate(@RequestBody SubordinateDTO subordinateDTO) {

        return clientUserManagementService.mySubordinate(subordinateDTO);
    }


    /**
     * 蓝星币账户
     * @param myAccountDTO
     * @return
     */
    @PostMapping("/myAccount")
    public AjaxResult myAccount(@RequestBody MyAccountDTO myAccountDTO) {

        return clientUserManagementService.myAccount(myAccountDTO);
    }


    /**
     * 获取回收订单详细信息
     */
    @GetMapping(value = "/getRecycleDetail/{recycleOrderNo}")
    @ApiOperation("获取回收订单详细信息")
    @ApiImplicitParam(name = "recycleOrderNo", value = "id", dataType = "String")
    public AjaxResult getRecycleDetail(@PathVariable("recycleOrderNo") String recycleOrderNo) {
        return AjaxResult.success(recycleOrderService.selectRecycleOrderById(recycleOrderNo)).put("detail",recycleOrderService.selectRecycleOrderDetailById(recycleOrderNo));
    }


    /**
     * 获取家政订单详细信息
     */
    @GetMapping(value = "/getHousekeepingDetail/{HKOrderNo}")
    @ApiOperation("获取家政订单详细信息")
    @ApiImplicitParam(name = "HKOrderNo", value = "家政订单订单号", dataType = "String")
    public AjaxResult getHousekeepingDetail(@PathVariable("HKOrderNo") String HKOrderNo) {
        return AjaxResult.success(housekeepingOrderService.selectHousekeepingOrderById(HKOrderNo)).put("detail",housekeepingOrderService.selectHousekeepingOrderDetailById(HKOrderNo));
    }


    /**
     * 回收/家政 订单评价表
     */
    @PostMapping("/saveOrderEvaluate")
    @ApiOperation("新增回收/家政 订单评价")
    public AjaxResult recycleEvaluateOrderPageQuery(@RequestBody OrderEvaluate orderEvaluate) {
        if (StringUtils.isNotNull(orderEvaluate.getOrderType()) && "1".equals(orderEvaluate.getOrderType())){
            //家政订单，需要获取服务内容
            orderEvaluate.setHousekeepingType("家政服务");
            HousekeepingOrder housekeepingOrder = housekeepingOrderService.getById(orderEvaluate.getOrderId());
            MerchantManagement merchantManagement = merchantManagementService.getById(housekeepingOrder.getMerchantId());
            orderEvaluate.setMerchantId(merchantManagement.getId().longValue());
            orderEvaluate.setMerchantName(merchantManagement.getMerchantName());
            housekeepingOrder.setStatus("7");
            housekeepingOrder.setUpdatedTime(new Date());
            housekeepingOrderService.updateById(housekeepingOrder);
        }else {
            orderEvaluate.setHousekeepingType("回收服务");
            RecycleOrder recycleOrder = recycleOrderService.getById(orderEvaluate.getOrderId());
            if (recycleOrder.getMerchantId() == 0){
                orderEvaluate.setMerchantId(0l);
                orderEvaluate.setMerchantName("蓝星人总部");
            }else {
                MerchantManagement merchantManagement = merchantManagementService.getById(recycleOrder.getMerchantId());
                orderEvaluate.setMerchantId(merchantManagement.getId().longValue());
                orderEvaluate.setMerchantName(merchantManagement.getMerchantName());
            }
            recycleOrder.setStatus("8");
            recycleOrder.setUpdatedTime(new Date());
            recycleOrderService.updateById(recycleOrder);
        }
        ClientUserManagement clientUserManagement = clientUserManagementService.getById(orderEvaluate.getClientId());
        orderEvaluate.setCreatedBy(clientUserManagement.getUserName());
        orderEvaluate.setCreatedTime(new Date());
        orderEvaluateService.saveorderEvaluate(orderEvaluate);


        return AjaxResult.success();
    }

    @PostMapping("/updateClient")
    @ApiOperation(value = "修改普通用户管理")
    public AjaxResult updateClient(@RequestBody ClientUserManagement clientUserManagement)
    {
        clientUserManagementService.updateById(clientUserManagement);
        return AjaxResult.success();
    }

    /**
     * 分页查询回收订单
     */
    @PostMapping("/recycleOrderPageQuery")
    @ApiOperation("分页查询回收订单")
    public AjaxResult recycleOrderPageQuery(@RequestBody RecycleOrderDTO queryDTO) {

        return recycleOrderService.miniClientGetClientRecycleOrder(queryDTO);
    }

    /**
     * 分页查询家政订单列表
     */
    @PostMapping("/housekeepingOrderPageQuery")
    @ApiOperation("分页查询家政订单列表")
    public AjaxResult housekeepingOrderPageQuery(@RequestBody HousekeepingOrderQueryDTO queryDTO) {

        return housekeepingOrderService.miniClientGetClientHousekeepingOrder(queryDTO);
    }


    /**
     * 新增用户地址
     */
    @Log(title = "新增用户地址", businessType = BusinessType.UPDATE)
    @PostMapping("/addAddress")
    @ApiOperation(value = "新增用户地址")
    public AjaxResult addAddress(@RequestBody ClientAddress clientAddress)
    {
        return clientUserManagementService.addAddress(clientAddress);
    }

    /**
     * 修改用户地址
     */
    @Log(title = "修改用户地址", businessType = BusinessType.UPDATE)
    @PostMapping("/updateAddress")
    @ApiOperation(value = "修改用户地址")
    public AjaxResult updateAddress(@RequestBody ClientAddress clientAddress)
    {
        return clientUserManagementService.updateAddress(clientAddress);
    }

    /**
     * 删除用户地址
     */
    @Log(title = "删除用户地址", businessType = BusinessType.UPDATE)
    @GetMapping("/deleteAddress/{ids}")
    @ApiOperation(value = "删除用户地址")
    @ApiImplicitParam(name = "ids", value = "id集合", dataType = "List")
    public AjaxResult deleteAddress(@PathVariable Long[] ids)
    {
        return clientUserManagementService.deleteAddress(ids);
    }

    /**
     * 获取用户地址列表
     */
    @Log(title = "获取用户地址列表", businessType = BusinessType.UPDATE)
    @GetMapping("/getClientAddress/{clientId}")
    @ApiOperation(value = "获取用户地址列表")
    public AjaxResult getClientAddress(@PathVariable(value = "clientId",required = true) Integer clientId){
        return clientUserManagementService.getClientAddress(clientId);
    }

    /**
    * 获取客户的积分订单列表
     */
    @PostMapping("/integralOrderPageQuery")
    @ApiOperation("分页查询回收订单")
    public AjaxResult integralOrderPageQuery(@RequestBody IntegralOrderDTO queryDTO) {

        return integralOrderService.miniClientGetClientIntegralOrder(queryDTO);
    }

    /**
     * 获取客户的收藏记录
     */
    @PostMapping("/collectCommodityPageQuery")
    @ApiOperation("获取客户的收藏记录")
    public AjaxResult collectCommodityPageQuery(@RequestParam(value = "clientId",required = true) Long clientId,
                                                @RequestParam(value = "type",required = true) String type,
                                                @RequestParam(value = "currentPage",required = true) Integer currentPage,
                                                @RequestParam(value = "pageSize",required = true) Integer pageSize) {

        if ("0".equals(type)) {
            return integralCommodityService.collectCommodityPageQuery(clientId, currentPage, pageSize);
        }else {
            return shopManagementService.collectShopPageQuery(clientId, currentPage, pageSize);
        }
    }

    /**
     * 客户许愿
     */
    @PostMapping("/clientPrayer")
    @ApiOperation("客户许愿")
    public AjaxResult clientPrayer(@RequestBody ClientPrayerManagement queryDTO) {

        return clientPrayerManagementService.saveClientPrayer(queryDTO);
    }

    /**
     * 根据客户ID分页查询客户许愿记录
     */
    @PostMapping("/prayerPageQueryByPhone")
    @ApiOperation("分页查询许愿管理列表")
    public IPage<ClientPrayerManagement> prayerPageQueryByPhone(@RequestBody ClientPrayerManagementQuery query) throws Exception{
        return clientPrayerManagementService.financePageQuery(query);
    }

}
